package sk.hackcraft.util.simulation.statistics;

public class Statistics
{
	public static final double QUANTILE_950 = 1.6449;
	public static final double QUANTILE_975 = 1.9600;
	
	private long counter;
	private double x;
	private double x2;
	
	public Statistics()
	{
		this.counter = 0;
		this.x = 0;
		this.x2 = 0;
	}
	
	public void addValue(double x)
	{
		this.x += x;
		this.x2 += Math.pow(x, 2);
		this.counter++;
	}
	
	public double getAverage()
	{
		if(this.counter == 0)
		{
			return 0;
		}
		
		return this.x / this.counter;
	}
	
	public long getCounter()
	{
		return counter;
	}
	
	public double getStandardDeviation()
	{
		double result = x2/counter - Math.pow(getAverage(), 2);
		return Math.sqrt(result);
	}
	
	public double[] getConfidenceInterval(double quantile)
	{
		double sqrtOfCounter = Math.sqrt(counter);
		
		double min = getAverage() - quantile * getStandardDeviation() / sqrtOfCounter;
		double max = getAverage() + quantile * getStandardDeviation() / sqrtOfCounter;
		
		double[] interval = {min, max};
		return interval;
	}
	
	public StatisticsItem createStatisticItem(double quantile)
	{
		return new StatisticsItem(getConfidenceInterval(quantile), getAverage(), getCounter());
	}
	
	public void clear()
	{
		this.counter = 0;
		this.x = 0;
		this.x2 = 0;
	}
}